<?php

class makeup_ctrl extends Controller
{


    //////每一个都进行单个提交

    public function __construct()
    {
        parent::__construct();

    }

    //查询试妆  前端用
    public function get_makeup_front()
    {
        if (!$this->ajax_err) {

            $pageno = (!empty($_REQUEST['pageNo'])) ? (int)($_REQUEST['pageNo']) : 1;
            $pagesize = (!empty($_REQUEST['pageSize'])) ? (int)($_REQUEST['pageSize']) : 400;
            $cat = (!empty($_REQUEST['cat']))? $_REQUEST['cat'] : '';
            $keyword = str_replace("+", "%20", urlencode($_REQUEST['keyWord']));
            //$keyword = addslashes(json_encode($_REQUEST['keyWord'])) == '\"\"'? '': addslashes(json_encode($_REQUEST['keyWord']));

            $_mdl = Loader::mdl('makeup_mdl');
            $data = $_mdl->get_makeup($pageno, $pagesize, $cat, $keyword);

            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }


    //查询试妆
    public function get_makeup()
    {
        if (!$this->ajax_err && $this->uid) {

            $pageno = (!empty($_REQUEST['pageNo'])) ? (int)($_REQUEST['pageNo']) : 1;
            $pagesize = (!empty($_REQUEST['pageSize'])) ? (int)($_REQUEST['pageSize']) : 40;
            $cat = (!empty($_REQUEST['cat']))? $_REQUEST['cat'] : '';
            $keyword = str_replace("+", "%20", urlencode($_REQUEST['keyWord']));
            //$keyword = addslashes(json_encode($_REQUEST['keyWord'])) == '\"\"'? '': addslashes(json_encode($_REQUEST['keyWord']));

            $_mdl = Loader::mdl('makeup_mdl');
            $data = $_mdl->get_makeup($pageno, $pagesize, $cat, $keyword);

            $items = array();
            foreach ($data['list'] as $one){

                $custom_tmpl_title = empty($one['custom_tmpl_title']) ? $one['tmpl_title'] : $one['custom_tmpl_title'];

                $custom_tmpl_img = empty($one['custom_tmpl_img']) ? $one['tmpl_img'] : $one['custom_tmpl_img'];

                if(!$items[$one['sku_id']]){
                    $one['tmpls'] = array();
                    array_push($one['tmpls'],array(
                        'tmpl_id'=>$one['tmpl_id'],
                        'tmpl_title'=>$custom_tmpl_title,
                        'tmpl_img'=>$custom_tmpl_img,
                    ));
                    unset($one['tmpl_id']);
                    unset($one['tmpl_title']);
                    unset($one['tmpl_img']);

                    $items[$one['sku_id']] = $one;
                }else{
                    array_push($items[$one['sku_id']]['tmpls'],array(
                        'tmpl_id'=>$one['tmpl_id'],
                        'tmpl_title'=>$custom_tmpl_title,
                        'tmpl_img'=>$custom_tmpl_img,
                    ));
                }

            }

            $data['list'] = $items;
            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }

    //保存试妆
    public function save_makeup()
    {
        if (!$this->ajax_err && $this->uid) {

            $tmpls = json_decode($_REQUEST['tmpls'],true);
            $item = $_REQUEST['item'];

            $item = json_decode($item,true);

            $_mdl = Loader::mdl('makeup_mdl');
            $all = array();
            foreach ($tmpls as $tmpl){
                $data = $_mdl->save_makeup($item['sku_id'],$item['title'], $item['type'], $item['color'], $item['custom_color'], $item['item_id'], $tmpl['tmpl_id'], $item['order_index'], $item['hot']);
                array_push($all,$data['id']);
            }

            //删除多余试妆数据
            $sql_makeups_id = $_mdl->get_makeups_id($item['type']);
            $del_ids = array_diff($sql_makeups_id['list'],$all);
            $_mdl->del_makeups_ids_id($del_ids);

            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }

    public function get_makeup_tmpl(){
        if (!$this->ajax_err) {

            $cat = $_REQUEST['cat'];
            $_mdl = Loader::mdl('makeup_mdl');
            $data = $_mdl->get_tmpls($cat);

            /*$items = array();
            foreach ($data['list'] as $one){
                $custom_tmpl_title = empty($one['custom_tmpl_title']) ? $one['tmpl_title'] : $one['custom_tmpl_title'];
                $custom_tmpl_img = empty($one['custom_tmpl_img']) ? $one['tmpl_img'] : $one['custom_tmpl_img'];

                array_push($items,array(
                    'type'=>$one['type'],
                    'tmpl_id'=>$one['tmpl_id'],
                    'tmpl_title'=>$custom_tmpl_title,
                    'tmpl_img'=>$custom_tmpl_img
                ));

            }*/

            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }


    //保存试妆材质
    public function save_makeup_tmpl()
    {
        if (!$this->ajax_err && $this->uid) {
            $item = $_REQUEST['item'];
            $item = json_decode($item,true);

            $_mdl = Loader::mdl('makeup_mdl');
            $data = $_mdl->save_makeup_tmpl($item['tmpl_id'], $item['custom_tmpl_title'], $item['custom_tmpl_img']);


            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }
}